#pragma once

#include "Log.hpp"
#include "InetAddr.hpp"

namespace SocketModule
{
    using namespace LogModule;

    class Socket
    {
    public:
        virtual ~Socket() = default;
        virtual void SocketOrDie() = 0;
        virtual void SetSocketOpt() = 0;
        virtual bool BindOrDie(int port) = 0;
        virtual bool ListenOrDie() = 0;
        virtual int Accepter(InetAddr *client) = 0;
        virtual void Close() = 0;
        virtual int Recv(std::string *out) = 0;
        virtual int Send(const std::string &in) = 0;
        virtual int Fd() = 0;

        void BuildTcpSocketMethod(int port)
        {
            SocketOrDie();
            SetSocketOpt();
            BindOrDie(port);
            ListenOrDie();
        }
    };

    class TcpSocket : public Socket
    {
    public:
        TcpSocket() : _sockfd(gdefaultsockfd)
        {}

        TcpSocket(int sockfd)
            : _sockfd(sockfd)
        {
        }

        virtual void SocketOrDie() override
        {
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                LOG(LogLevel::ERROR) << "socket error..." << _sockfd;
                exit(SOCKET_ERR);
            }
            LOG(LogLevel::DEBUG) << "socket create success..." << _sockfd;
        }

        virtual void SetSocketOpt() override
        {
            int opt = 1;
            int n = ::setsockopt(_sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
            (void)n;
        }

        virtual bool BindOrDie(int port) override
        {
            if (_sockfd == gdefaultsockfd)
                return false;

            InetAddr addr(port);
            int n = ::bind(_sockfd, addr.NetAddr(), addr.NetAddrLen());
            if (n < 0)
            {
                LOG(LogLevel::ERROR) << "bind error..." << _sockfd;
                exit(BIND_ERR);
            }
            LOG(LogLevel::DEBUG) << "bind create success..." << _sockfd;
            return true;
        }

        virtual bool ListenOrDie() override
        {
            if (_sockfd == gdefaultsockfd)
                return false;

            int n = ::listen(_sockfd, gbacklog);
            if (n < 0)
            {
                LOG(LogLevel::ERROR) << "listen error..." << _sockfd;
                exit(LISTEN_ERR);
            }
            LOG(LogLevel::DEBUG) << "listen create success..." << _sockfd;
            return true;
        }

        virtual int Accepter(InetAddr *client) override
        {
            if (_sockfd == gdefaultsockfd)
                return false;

            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);

            int newsockfd = ::accept(_sockfd, CONV(&peer), &len);
            if (newsockfd < 0)
            {
                LOG(LogLevel::ERROR) << "listen error..." << _sockfd;
                return -1;
            }
            LOG(LogLevel::DEBUG) << "listen create success..." << _sockfd;
            client->SetAddr(peer, len);
            return newsockfd;
        }

        virtual void Close() override
        {
            if(_sockfd == gdefaultsockfd)
                return ;
            
                ::close(_sockfd);
        }

        virtual int Recv(std::string * out) override
        {
            char buffer[1024];
            auto size = ::recv(_sockfd, buffer,sizeof(buffer), 0);
            if(size  > 0)
            {
                buffer[size]  = 0;
                *out = buffer;
            }
            return size;
        }

        virtual int Send(const std::string &in) override
        {
            auto size = ::send(_sockfd, in.c_str(), sizeof(in), 0);
            return size;
        }

        virtual int Fd() override
        {
            return _sockfd;
        }

        ~TcpSocket()
        {
        }

    private:
        int _sockfd;
    };

    // for test
    // int main()
    // {
    //     Socket* sk = new TcpSocket();
    //     sk->BuildTcpSocketMethod(8080);
    // }
}